<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>03_计算器练习</title>
</head>
<body>
<input type="text" placeholder="请输入数字1" id="num1"><br>
<input type="text" placeholder="请输入数字1" id="num2"><br>
<button onclick="calc('+')">加</button>
<button onclick="calc('-')">减</button>
<button onclick="calc('*')">乘</button>
<button onclick="calc('/')">除</button>
<h4>计算结果为：<span></span></h4>
<script>
    function calc(o) {
        let num1 = document.querySelector('#num1').value;
        let num2 = document.querySelector('#num2').value;
        num1 = parseFloat(num1);
        num2 = parseFloat(num2);
        let spanE = document.querySelector('span');
        switch (o) {
            case '+':
                spanE.innerHTML = num1 + num2;
                break;
            case '-':
                spanE.innerHTML = num1 - num2;
                break;
            case '*':
                spanE.innerHTML = num1 * num2;
                break;
            case '/':
                if (num2 === 0){
                    alert('除数不能为0');
                }else {
                    spanE.innerHTML = num1 / num2;
                }
        }

    }
</script>
</body>
</html>